/**
 * 
 */
package br.gov.sjc.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import br.gov.sjc.classes.Documentos;
import br.gov.sjc.classes.Endereco;

/**
 * @author rodrigo.ramos
 *
 */
public class EnderecoDAO extends SQLiteOpenHelper {
	private static final String TABELA = "tab_Endereco";
	private static final String DATABASE_NAME = BaseDAO.DATABASE_NAME;
	private static final int DATABASE_VERSION = BaseDAO.DATABASE_VERSION;
	private static final String[] COLS = {"_id "," SiasN ","AlterouEndereco "," EndCodigo ",
		" Endereco "," Numero "," Ap "," Bloco "," Complemento "," Referencia "," Bairro ",
		"CEP "," CondicaoMoradia "," TipoConstrucao "," Area "," NumComodos "," NumMoradores ",
		" NumNucleosFamiliares "," TMDFCasaAno ","TMDFCasaMes ","TMDFCasaDia "," TMDFBairroAno ",
		"TMDFBairroMes ","TMDFBairroDia "," TMDFMunicipioAno ","TMDFMunicipioMes","TMDFMunicipioDia"};
	
	public EnderecoDAO(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "CREATE TABLE IF NOT EXISTS "+TABELA+"(" +
				"_id INTEGER PRIMARY KEY, " +
				"SiasN INT," +
				"AlterouEndereco TEXT, " +
				"EndCodigo TEXT, " +
				"Endereco TEXT, " +
				"Numero TEXT, " +
				"Ap TEXT, " +
				"Bloco TEXT, " +
				"Complemento TEXT, " +
				"Referencia TEXT, " +
				"Bairro TEXT, " +
				"CEP TEXT, " +
				"CondicaoMoradia TEXT, " +
				"TipoConstrucao TEXT, " +
				"Area TEXT, " +
				"NumComodos TEXT, " +
				"NumMoradores TEXT, " +
				"NumNucleosFamiliares TEXT, " +
				"TMDFCasaAno TEXT, " +
				"TMDFCasaMes TEXT, " +
				"TMDFCasaDia TEXT, " +
				"TMDFBairroAno TEXT, " +
				"TMDFBairroMes TEXT, " +
				"TMDFBairroDia TEXT, " +
				"TMDFMunicipioAno TEXT, " +
				"TMDFMunicipioMes TEXT, " +
				"TMDFMunicipioDia TEXT" +
				")";
		db.execSQL(sql);/**/
		Log.i("EnderecoDAO","onCreate");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		 db.execSQL("DROP TABLE IF EXISTS " + EnderecoDAO.TABELA);
		 this.onOpen(db);
		
		Log.i("EnderecoDAO","onUpgrade");
	}
	
	public void inserir(Endereco end){ 
		ContentValues valores = new ContentValues();
		//valores.put("_id", end.get_id());
		valores.put("SiasN", end.getSiasN());
		valores.put("AlterouEndereco", end.getAlterouEndereco());
		valores.put("EndCodigo", end.getEndCodigo());
		valores.put("Endereco", end.getEndereco());
		valores.put("Numero", end.getNumero());
		valores.put("Ap", end.getAp());
		valores.put("Bloco", end.getBloco());
		valores.put("Complemento", end.getComplemento());
		valores.put("Referencia", end.getReferencia());
		valores.put("Bairro", end.getBairro());
		valores.put("CEP", end.getCEP());
		valores.put("CondicaoMoradia", end.getCondicaoMoradia());
		valores.put("TipoConstrucao", end.getTipoConstrucao());
		valores.put("Area", end.getArea());
		valores.put("NumComodos", end.getNumComodos());
		valores.put("NumMoradores", end.getNumMoradores());
		valores.put("NumNucleosFamiliares", end.getNumNucleosFamiliares());
		valores.put("TMDFCasaAno", end.getTMDFCasaAno());
		valores.put("TMDFCasaMes", end.getTMDFCasaMes());
		valores.put("TMDFCasaDia", end.getTMDFCasaDia());
		valores.put("TMDFBairroAno", end.getTMDFBairroAno());
		valores.put("TMDFBairroMes", end.getTMDFBairroMes());
		valores.put("TMDFBairroDia", end.getTMDFBairroDia());
		valores.put("TMDFMunicipioAno", end.getTMDFMunicipioAno());
		valores.put("TMDFMunicipioMes", end.getTMDFMunicipioMes());
		valores.put("TMDFMunicipioDia", end.getTMDFMunicipioDia());
		
		getWritableDatabase().insert(TABELA, null, valores);
	}
	
	public List<Endereco> getLista(){
		Cursor c = getWritableDatabase().query(TABELA, COLS, null, null, null, null, null);
		List<Endereco> lista = new ArrayList<Endereco>();
		while(c.moveToNext()){
			Endereco end = new Endereco();
			end.set_id(c.getInt(0));
			end.setSiasN(c.getInt(1));
			end.setAlterouEndereco(c.getString(2));
			end.setEndCodigo(c.getString(3));
			end.setEndereco(c.getString(4));
			end.setNumero(c.getString(5));
			end.setAp(c.getString(6));
			end.setBloco(c.getString(7));
			end.setComplemento(c.getString(8));
			end.setReferencia(c.getString(9));
			end.setBairro(c.getString(10));
			end.setCEP(c.getString(11));
			end.setCondicaoMoradia(c.getString(12));
			end.setTipoConstrucao(c.getString(13));
			end.setArea(c.getString(14));
			end.setNumComodos(c.getString(15));
			end.setNumMoradores(c.getString(16));
			end.setNumNucleosFamiliares(c.getString(17));
			end.setTMDFCasaAno(c.getString(18));
			end.setTMDFCasaMes(c.getString(19));
			end.setTMDFCasaDia(c.getString(20));
			end.setTMDFBairroAno(c.getString(21));
			end.setTMDFBairroMes(c.getString(22));
			end.setTMDFBairroDia(c.getString(23));
			end.setTMDFMunicipioAno(c.getString(24));
			end.setTMDFMunicipioMes(c.getString(25));
			end.setTMDFMunicipioDia(c.getString(26));
			
			lista.add(end);
		}
		c.close();
		
		return lista;
		
	}

	public Endereco getById(int posicao) {
		Cursor c = getWritableDatabase().query(TABELA, COLS, "_id=?", new String[]{"" + posicao}, null, null, null);
		c.moveToFirst();
		
		Endereco end = new Endereco();
		end.set_id(c.getInt(0));
		end.setSiasN(c.getInt(1));
		end.setAlterouEndereco(c.getString(2));
		end.setEndCodigo(c.getString(3));
		end.setEndereco(c.getString(4));
		end.setNumero(c.getString(5));
		end.setAp(c.getString(6));
		end.setBloco(c.getString(7));
		end.setComplemento(c.getString(8));
		end.setReferencia(c.getString(9));
		end.setBairro(c.getString(10));
		end.setCEP(c.getString(11));
		end.setCondicaoMoradia(c.getString(12));
		end.setTipoConstrucao(c.getString(13));
		end.setArea(c.getString(14));
		end.setNumComodos(c.getString(15));
		end.setNumMoradores(c.getString(16));
		end.setNumNucleosFamiliares(c.getString(17));
		end.setTMDFCasaAno(c.getString(18));
		end.setTMDFCasaMes(c.getString(19));
		end.setTMDFCasaDia(c.getString(20));
		end.setTMDFBairroAno(c.getString(21));
		end.setTMDFBairroMes(c.getString(22));
		end.setTMDFBairroDia(c.getString(23));
		end.setTMDFMunicipioAno(c.getString(24));
		end.setTMDFMunicipioMes(c.getString(25));
		end.setTMDFMunicipioDia(c.getString(26));
		
		c.close();
		return end;
	}

	public void alterar(Endereco end) {
		ContentValues valores = new ContentValues();
		//valores.put("_id", end.get_id());
		valores.put("SiasN", end.getSiasN());
		valores.put("AlterouEndereco", end.getAlterouEndereco());
		valores.put("EndCodigo", end.getEndCodigo());
		valores.put("Endereco", end.getEndereco());
		valores.put("Numero", end.getNumero());
		valores.put("Ap", end.getAp());
		valores.put("Bloco", end.getBloco());
		valores.put("Complemento", end.getComplemento());
		valores.put("Referencia", end.getReferencia());
		valores.put("Bairro", end.getBairro());
		valores.put("CEP", end.getCEP());
		valores.put("CondicaoMoradia", end.getCondicaoMoradia());
		valores.put("TipoConstrucao", end.getTipoConstrucao());
		valores.put("Area", end.getArea());
		valores.put("NumComodos", end.getNumComodos());
		valores.put("NumMoradores", end.getNumMoradores());
		valores.put("NumNucleosFamiliares", end.getNumNucleosFamiliares());
		valores.put("TMDFCasaAno", end.getTMDFCasaAno());
		valores.put("TMDFCasaMes", end.getTMDFCasaMes());
		valores.put("TMDFCasaDia", end.getTMDFCasaDia());
		valores.put("TMDFBairroAno", end.getTMDFBairroAno());
		valores.put("TMDFBairroMes", end.getTMDFBairroMes());
		valores.put("TMDFBairroDia", end.getTMDFBairroDia());
		valores.put("TMDFMunicipioAno", end.getTMDFMunicipioAno());
		valores.put("TMDFMunicipioMes", end.getTMDFMunicipioMes());
		valores.put("TMDFMunicipioDia", end.getTMDFMunicipioDia());
		
		getWritableDatabase().update(TABELA, valores, "_id=?", new String[]{"" + end.get_id()});
		
	}

	public boolean recreateTable(SQLiteDatabase database) {
		boolean retorno = false;
		try {
			database.execSQL("DROP TABLE IF EXISTS " + TABELA);
	    	onCreate(database);
	    	retorno = true;
		}catch(Exception e){
			Log.e(TABELA,"ERRO MENSAGEM:"+e.getMessage());
        }
		return retorno;
	}

}
